Java 12 Features Intro
π Java 12 β What's New and Exciting? πβ
Java 12 (released on March 19, 2019) brought some cool new features and improvements for developers and architects. Letβs dive in and explore what's fresh and exciting in the world of Java! π
1οΈβ£ Collectors.teeing() in Stream API π―β
Ever wished you could process a stream into two different collectors and then merge the results? Well, Java 12 says, "Wish granted!" π§ββοΈ
The new teeing()
collector allows you to do exactly that! It takes two collectors, processes the stream through both, and then merges the results using a function.
Example πβ
Want to find both the highest and lowest salary in a single statement? Here you go:
SalaryRange salaryRange = Stream
.of(56700, 67600, 45200, 120000, 77600, 85000)
.collect(teeing(
minBy(Integer::compareTo),
maxBy(Integer::compareTo),
SalaryRange::fromOptional));
How cool is that? π€©